﻿using DataServer.CommonLib.Constants;
using DataServer.CommonLib.Models.DataSource;
using Newtonsoft.Json;
using Swimj.CommonLib.Base;
using Swimj.Core.Extensions;

namespace Config.RazorLib.Services
{
    public class DataSourceService : IDataSourceService
    {
        private readonly IHttpClientFactory _httpClientFactory;

        public DataSourceService(IHttpClientFactory httpClientFactory)
        {
            _httpClientFactory = httpClientFactory;
        }

        public async Task<PaginatedResult<DataSourceInfoResponseDto>?> QueryDataSourcesByPage(
            DataSourceSearchRequestDto model)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("dataserverApi");
                var response = await httpClient.GetAsync(model.ToQueryString(ApiRoutes.DataSource.QueryDataSources));
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<PaginatedResult<DataSourceInfoResponseDto>>(resultJson);
                    }
                }

                return null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public Task<Result<DataSourceInfoResponseDto>> UpdateDataSource(Guid id, DataSourceEditRequestDto editRequestDto)
        {
            throw new NotImplementedException();
        }

        public Task<Result<DataSourceInfoResponseDto>> CreateDataSource(DataSourceEditRequestDto editRequestDto)
        {
            throw new NotImplementedException();
        }

        public Task<Result<DataSourceInfoResponseDto>> GetDataSource(Guid id)
        {
            throw new NotImplementedException();
        }
    }
}